<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Log;


class Scholarmenuauth extends AdminBase{

    protected function _initialize()
    {
        parent::_initialize();
        //if ($this->request->isGet()) {
            //$this->assign('institute', model('scholar')->field('institute')->group('institute')->select());
            //$this->assign('professionalTitle', model('scholar')->field('professional_title')->group('professional_title')->select());
        //}
    }


    /**
     * 学者列表(未绑定)
     */
    public function searchScholar()
    {
        $param = $this->request->param();
        $where = [];
        $whereOr = [];
        if (isset($param['name'])) {
            $whereOr['scholar_name'] = ['like', "%" . $param['name'] . "%"];
            //$whereOr['email'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['phone'] = ['like', "%" . $param['name'] . "%"];
        }
        //$notinsql = model('scholarMenuAuth')->field('user_no')->buildSql();
        $notinsql = Db::table('scholar_menu_auth')->field('user_no')->buildSql();
        //$list = model('scholar')
        $list = Db::table('scholar')
            ->whereOr(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->where(function ($query) use ($where) {
                $query->where($where);
            })
            ->where('kid not in'.$notinsql)->limit('0,10')->select();
            //->paginate(config('page_number'), false, ['query' => $param]);
        //var_dump($list);
        $obj['list'] = $list;
        //var_dump($obj);
        return $this->result($obj, 1);
    }


    /**
     * 权限首页
     */
    public function index(){
        $param = $this->request->param();
        $whereOr = [];
        if (isset($param['name'])) {
            $whereOr['scholar_name'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['phone'] = ['like', "%" . $param['name'] . "%"];
        }
        $list = Db::table('scholar_menu_auth')->alias('a')
            ->join('scholar b','a.user_no = b.kid')
            ->field('a.id,b.scholar_name,b.phone,a.fusion_data_warehouse,a.academic_portrait,a.technology_situation_perceive')
            ->whereOr(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->paginate(config('page_number'), false, ['query' => $param]);;
        return $this->fetch('index', ['list' => $list]);
        //var_dump($list);
    }

    /**
     * 新增学者权限
     */
    public function addScholarAuth(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $obj=json_decode($param['list'],TRUE);
            if (is_array($obj)) {
                $dbData = [];
                foreach ($obj as $k => $v) {
                    $dbData[$k]['user_no'] = $v['user_no'];
                    $dbData[$k]['fusion_data_warehouse'] = $v['fusion_data_warehouse'];
                    $dbData[$k]['academic_portrait'] = $v['academic_portrait'];
                    $dbData[$k]['technology_situation_perceive'] = $v['technology_situation_perceive'];
                }
                $msg = Db::table('scholar_menu_auth')->insertAll($dbData, true);
                if($msg > 0){
                    //$this->success('添加成功', url('admin/scholarmenuauth/index'));
                    $this->result('', 1, '添加成功');
                }
            }
        }
        return $this->fetch('addScholarAuth');
    }


    /**
     * 删除学者权限
     */
    public function delScholarAuth(){
        if ($this->request->isPost()) {
            if ($this->delete('scholarMenuAuth', $this->request->param()) === true) {
                insert_admin_log('删除了学者权限');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }

    /**
     * 修改学者权限
     */
    public function modifyScholarAuth(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['update_time'] = date("Y-m-d H:i:s");
            if ($this->update('scholarMenuAuth', $param, false, input('_verify', true)) === true) {
                insert_admin_log('修改了学者权限');
                $this->success('修改成功', url('admin/scholarmenuauth/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
}
